#region License

// This file is part of Grawlix.
// 
// Grawlix is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
// Grawlix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public
// License along with Grawlix.  If not, see
// <http://www.gnu.org/licenses/>.

#endregion

using System;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace Grawlix.Services
{
	/// <summary>
	/// Resolves to a service of the given type.
	/// </summary>
	/// <typeparam name="T">The type of service to resolve to.
	/// </typeparam>
	[Serializable]
	public class ServiceObjectReference<T> : IObjectReference
	{
		/// <summary>
		/// Returns the real object that should be deserialized, rather
		/// than the object that the serialized stream specifies.
		/// </summary>
		/// <param name="context">The 
		/// <see cref="T:System.Runtime.Serialization.StreamingContext"/>
		/// from which the current object is deserialized.</param>
		/// <returns>
		/// Returns the actual object that is put into the graph.
		/// </returns>
		/// <exception cref="T:System.Security.SecurityException">
		/// The caller does not have the required permission. The call
		/// will not work on a medium trusted server.
		/// </exception>
		[SecurityPermission(SecurityAction.LinkDemand,
			Flags = SecurityPermissionFlag.SerializationFormatter)]
		public object GetRealObject(StreamingContext context)
		{
			IGameKernel kernel =
				GameKernelStreamingContext.GetContextKernel(context);
			return kernel.ResolveService<T>();
		}
	}
}